Regarding Concatenation of Strings


DXLUserbsr - Thu May 02 09:44:31 EDT 2013

Re: Regarding Concatenation of Strings
llandale - Thu May 02 11:58:12 EDT 2013

Let me guess what was deleted.

each concatenated string takes up space in the "string table" within the current DXL context.  Some clever hash-function can find a string in that table, and insert it if it does not.  DXL interpreter is not clever enough to realize that a string not saved in a variable or some allocated container cannot be "used" again when the current function ends; thus the string table persists.

I believe the following ...

  • string Result = "A" "B" "C" "D"

.. will result in these strings end up in the string table (in this order):

  • D
  • C
  • CD
  • B
  • BCD
  • A
  • ABCD

Taking up 13 bytes not 4.

I think this..

  • string Result = ""
  • for (i=0; i<1000; i++)
  • {  Result = Result "A"
  • }

.. ends up with 500,000 bytes wasted in the string table; just to create a 1000 byte string.

You must therefore avoid concatenation inside loops.  Buffers are good for that:

  • Buffer buf = create
  • for (i=0; i<1000; i++)
  • {  buf += "A"
  • }
  • string Result = stringOf(buf)
  • delete(buf)

I think puts 1001 bytes in the table.  It runs faster as well, but that is insigificant.

The function "tempStringOf" does NOT put the string in the string table; instead returns the address in the buffer where the string can be found.  if you subsequently change the Buffer contents then you have also changed the variable contents.  Thus, tempStringOf() can be used only if you immediately ignore the string or displose of the string (print, write, set attr-value, set on dialog).

-Louie

Re: Regarding Concatenation of Strings
DXLUserbsr - Fri May 03 15:16:07 EDT 2013

llandale - Thu May 02 11:58:12 EDT 2013

Let me guess what was deleted.

each concatenated string takes up space in the "string table" within the current DXL context.  Some clever hash-function can find a string in that table, and insert it if it does not.  DXL interpreter is not clever enough to realize that a string not saved in a variable or some allocated container cannot be "used" again when the current function ends; thus the string table persists.

I believe the following ...

  • string Result = "A" "B" "C" "D"

.. will result in these strings end up in the string table (in this order):

  • D
  • C
  • CD
  • B
  • BCD
  • A
  • ABCD

Taking up 13 bytes not 4.

I think this..

  • string Result = ""
  • for (i=0; i<1000; i++)
  • {  Result = Result "A"
  • }

.. ends up with 500,000 bytes wasted in the string table; just to create a 1000 byte string.

You must therefore avoid concatenation inside loops.  Buffers are good for that:

  • Buffer buf = create
  • for (i=0; i<1000; i++)
  • {  buf += "A"
  • }
  • string Result = stringOf(buf)
  • delete(buf)

I think puts 1001 bytes in the table.  It runs faster as well, but that is insigificant.

The function "tempStringOf" does NOT put the string in the string table; instead returns the address in the buffer where the string can be found.  if you subsequently change the Buffer contents then you have also changed the variable contents.  Thus, tempStringOf() can be used only if you immediately ignore the string or displose of the string (print, write, set attr-value, set on dialog).

-Louie

Thankyou very much. It works now.

But I need to look for column title for three attributes , fetch that column title to Object Text.

How to fetch the column title? Do you have any code snippet for that?

Re: Regarding Concatenation of Strings
llandale - Sun May 05 13:39:14 EDT 2013

DXLUserbsr - Fri May 03 15:16:07 EDT 2013

Thankyou very much. It works now.

But I need to look for column title for three attributes , fetch that column title to Object Text.

How to fetch the column title? Do you have any code snippet for that?

Not sure what you are asking.  Does this help?

  • Module mHome = current
  • Column col
  • string Title, Attr, Name, DXL
  • bool  IsDXL, IsMain
  • for col in mHome do
  • {  Title  = title(col)
  •    Attr   = attrName(col)
  •    DXL    = dxl(col)
  •    IsMain = main(col)
  •    if    (!null Attr)  then Name = Attr
  •    elseif(!null DXL)  then Name = "Layout"
  •    elseif(IsMain)   then Name = "Main"
  •        else Name = "Louie made a Mistake"
  •    print Name "\t*" Title "*\t[" (text(col, current Object))[0:50] "]\n"
  • }

-Louie